package cn.easy.kitchen.service;

import java.util.Map;
import java.util.List;

import cn.easy.base.service.BaseService;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.easy.base.bean.PageBean;
import cn.easy.kitchen.domain.MaterialType;
import cn.easy.kitchen.repository.MaterialTypeDao;

@Component
public class MaterialTypeService extends BaseService<MaterialType> {
    @Autowired
    private MaterialTypeDao	materialTypeDao;

    public Page<MaterialType> search(Map<String, Object> searchParams, PageBean pageBean) {
        return materialTypeDao.findAll(spec(searchParams), pageBean.toPageRequest(new Sort(Direction.DESC, "createdAt")));
    }

    public List<MaterialType> search(Map<String, Object> searchParams, Sort... sort) {
        return materialTypeDao.findAll(spec(searchParams), sort == null || sort.length == 0  ? new Sort(Direction.DESC, "createdAt") : sort[0]);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(String id) {
        materialTypeDao.delete(id);
    }

    public MaterialType getOne(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        return materialTypeDao.findOne(id);
    }

    public List<MaterialType> findAll(List<String> idList) {
        return Lists.newArrayList(materialTypeDao.findAll(idList));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public MaterialType save(MaterialType materialType) {
        return materialTypeDao.save(materialType);
    }

}


